package demo.c1;

/**
 * DeamonThreadDemo
 *
 * @author chisheng
 */
public class DeamonThreadDemo {

    /* The Java Virtual Machine exits when the only threads running are all daemon threads. */

    @SuppressWarnings("all")
    public static void main(String[] args) throws InterruptedException {
        final Thread thread = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        /* Marks this thread as either a daemon thread or a user thread. */
        thread.setDaemon(true);

        /* Tests if this thread is a daemon thread. */
        System.out.println(thread.getName() + " is deamon: " + thread.isDaemon());

        // main thread area
        thread.start();
        Thread.sleep(2_000L);
        System.out.println("main thread terminated.");
    }

}
